package com.business.system.action;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.web.utils.WebUtils;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.business.system.bean.PlatformInfoBean;
import com.business.system.util.DBTools;
import com.business.system.util.FileUtil;

/**
 * @author: xudy
 * @date: 2018/04/15 22:03
 * @description:
 */
@RestController
@RequestMapping("system")
public class SystemAction extends BaseAction{

    @RequestMapping(value = "/getInfo.do",method = RequestMethod.GET)
    public PlatformInfoBean getInfo(HttpServletRequest request, Service service){
        PlatformInfoBean info = DBTools.getBean(service,PlatformInfoBean.class,"select * from platform_info");
        return info;
    }

    @RequestMapping(value = "/saveInfoNoFile.do",method = RequestMethod.POST)
    public boolean saveInfoNoFile(HttpServletRequest request, Service service,@ModelAttribute PlatformInfoBean bean){
        return DBUtils.update(service,bean)>0;
    }

    @RequestMapping(value = "/saveInfo.do",method = RequestMethod.POST)
    public boolean saveInfo(HttpServletRequest request, Service service){
        try{
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            //额外的数据
            Map<String,Object> data = WebUtils.getRequestData(request);
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            String ctxPath = FileUtil.getUploadFilePath();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            String ymd = sdf.format(new Date());
            ctxPath += File.separator + ymd + File.separator;
            // 创建文件夹
            File file = new File(ctxPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            String fileName = null;
            UUID uuid = null;
            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                // 上传文件
                MultipartFile mf = entity.getValue();
                fileName = mf.getOriginalFilename();// 获取原文件名
                fileName = fileName.substring(fileName.lastIndexOf("."));
                uuid = UUID.randomUUID();
                File uploadFile = new File(ctxPath + uuid + fileName);
                try {
                    FileCopyUtils.copy(mf.getBytes(), uploadFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            String path = ymd + "/" + uuid + fileName;
            // 将图片转换成网络地址
            PlatformInfoBean info = new PlatformInfoBean();
            info.setId(data.get("id")==null?null:data.get("id").toString());
            info.setCardName(data.get("cardName").toString());
            info.setCardNo(data.get("cardNo").toString());
            if(fileMap.entrySet().size()>0){
                info.setCardIcon(path);
            }
            int i = DBUtils.update(service,info);
            return i>0;
        }
        catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}